package com.rongshu.view.login.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.login.contract.InputAuthCodeView;
import com.rongshu.view.login.entity.PasswordLogin;
import com.rongshu.view.login.entity.VerificationCode;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2019/7/17.
 * description：
 */
public class InputAuthCodePresenter extends BasePresenter<InputAuthCodeView> {

    public InputAuthCodePresenter(Context context) {
        super(context);
    }

    /**
     * 获取手机验证码
     */
    public void getPhoneVerficationCode(String mobile, int questAuthCodeType) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("type", questAuthCodeType);
        RetorfitService.getPhoneVerficationCode(mobile, questAuthCodeType, Common.encryptionSign(map))
                .compose(getView().<VerificationCode>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<VerificationCode>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(VerificationCode verificationCode) {
                        getView().onCallBackAuthCode(verificationCode);
                    }
                });
    }

    /**
     * 用户手机号和短信验证码登陆
     */
    public void getUserRegisterforMobileCode(String phone, String code) {
        //极光注册id
        String registerId = UserInfoUtils.getRegistrationId();
        Map<String, Object> login_map = new HashMap<>();
        login_map.put("mobile", phone);
        login_map.put("code", code);
        login_map.put("registrationid", TextUtils.isEmpty(registerId) ? "" : registerId);//推送ID，推送用
        RetorfitService.getUserRegisterforMobileCode(phone, code, registerId, Common.encryptionSign(login_map))
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<PasswordLogin>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(PasswordLogin passwordLogin) {
                        getView().onCallBackLoginSuccess(passwordLogin);
                    }
                });
    }

    /**
     * 绑定手机号码
     */
    public void getBindingMobile(int user_id, String mobile, String sms_code, String key, String sign) {
        RetorfitService.getBindingMobile(user_id, mobile, sms_code, key, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        if (getView() != null) {
                            getView().onBindPhone(bean);
                        }
                    }
                });
    }
}
